class PartPurchase < ActiveRecord::Base
	belongs_to :supplier
	belongs_to :user
	has_many :part_purchase_details, dependent: :destroy

	enum status: %w(新建 确定)
	enum payment_status: %w(未付款 已付款)

	validates_presence_of   :supplier_id
	after_create :gen_code

	def gen_code
	  code = rand(100000).to_s.rjust(6, "0")
	  update_attributes(code: code)
	end

	def update_purchase_status
       add_item = 0
       part_purchase_details.each do |i|
         add_item = add_item + i.sub_total
       end
       update(total: add_item, status: 1)
	end

end
